package com.ymm.lulutong.domain.impl;

import com.ymm.lulutong.constant.ProfileCodeEnum;
import com.ymm.lulutong.constant.ResultCodeEnum;
import com.ymm.lulutong.constants.VipRankEnum;
import com.ymm.lulutong.domain.ShipperVipDomainService;
import com.ymm.lulutong.domain.ShipperWalletDomainService;
import com.ymm.lulutong.entity.Shipper;
import com.ymm.lulutong.mapper.ShipperProfileMapper;
import com.ymm.lulutong.mapper.ShipperVipCardMapper;
import com.ymm.lulutong.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Objects;

//TODO

/**
 * @author guorui
 */
@Slf4j
@Service("shipperVIPDomainServiceImpl")
public class ShipperVipDomainServiceImpl implements ShipperVipDomainService {
    @Resource
    private ShipperVipCardMapper shipperVipCardMapper;

    @Resource
    private ShipperProfileMapper shipperProfileMapper;

    @Resource
    private ShipperWalletDomainService shipperWalletDomainService;

    /**
     * 白银会员积分标准
     */
    private static final Integer SILVER_VIP_POINT_STANDARD = 1000;
    /**
     * 黄金会员积分标准
     */
    private static final Integer GOLD_VIP_POINT_STANDARD = 2000;
    /**
     * 铂金会员积分标准
     */
    private static final Integer PLATINUM_VIP_POINT_STANDARD = 5000;
    /**
     * 钻石会员积分标准
     */
    private static final Integer DIAMONDS_VIP_POINT_STANDARD = 8000;
    /**
     * 白银会员返现比例
     */
    private static final double SILVER_VIP_CASH_BACK_PERCENTAGE = 0.03;
    /**
     * 黄金会员返现比例
     */
    private static final double GOLD_VIP_CASH_BACK_PERCENTAGE = 0.04;
    /**
     * 铂金会员返现比例
     */
    private static final double PLATINUM_VIP_CASH_BACK_PERCENTAGE = 0.05;
    /**
     * 钻石会员返现比例
     */
    private static final double DIAMONDS_VIP_CASH_BACK_PERCENTAGE = 0.06;


    /**
     * 会员 更新会员等级和积分 返现
     * @param shipperId 货主Id
     * @param money 发货金额
     * @param cargoId 货源Id，可以为空
     * @param orderId 订单Id，可以为空
     * @return
     */
    @Override
    @Transactional
    public Result<Shipper> updateVipScoreAndLevelAndCashBack(Long shipperId, Integer money, Long cargoId, Long orderId) throws Exception {
        if (shipperId == null || money == null) {
            log.info("参数错误");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        Shipper shipper;
        shipper = shipperProfileMapper.selectById(shipperId);
        if (shipper == null) {
            log.info("不存在的用户");
            return Result.fail(null, ProfileCodeEnum.SHIPPER_NOT_EXIST.getCode(), ProfileCodeEnum.SHIPPER_NOT_EXIST.getMessage());
        }
        if (!shipper.getValid()) {
            log.info("账号已被注销");
            return Result.fail(null, ProfileCodeEnum.ACCOUNT_CANCELLED.getCode(), ProfileCodeEnum.ACCOUNT_CANCELLED.getMessage());
        }
        //增加积分
        Integer vipScore = shipperVipCardMapper.getVipScore(shipperId);
        vipScore += money / 100;
        //更新会员积分
        shipperVipCardMapper.updateVipScore(shipperId, vipScore);
        Integer vipLevel = shipperVipLevelRefresh(vipScore);
        //按照增加后的信用分确定货主会员等级
        shipperVipCardMapper.updateVipLevel(shipperId, vipLevel);
        //获取货主的会员等级并返现
        Integer vipType = shipperVipCardMapper.getVipType(shipperId);
        Integer cashBackMoney = shipperCashBack(vipType, money);
        log.info("会员等级" + vipType);
        log.info("返现数额" + cashBackMoney);
        shipperWalletDomainService.cashBack(shipperId, cashBackMoney, cargoId, orderId);
        shipper = shipperProfileMapper.selectById(shipperId);
        return Result.success(shipper);
    }

    /**
     * 更新会员等级
     *
     * @param vipPoint 会员积分
     * @return 会员等级
     */
    private Integer shipperVipLevelRefresh(Integer vipPoint) {
        Integer shipperVipLevel = VipRankEnum.VIP_RANK_1.getCode();
        if (vipPoint >= SILVER_VIP_POINT_STANDARD && vipPoint < GOLD_VIP_POINT_STANDARD) {
            shipperVipLevel = VipRankEnum.VIP_RANK_2.getCode();
        } else if (vipPoint >= GOLD_VIP_POINT_STANDARD && vipPoint < PLATINUM_VIP_POINT_STANDARD) {
            shipperVipLevel = VipRankEnum.VIP_RANK_3.getCode();
        } else if (vipPoint >= PLATINUM_VIP_POINT_STANDARD && vipPoint < DIAMONDS_VIP_POINT_STANDARD) {
            shipperVipLevel = VipRankEnum.VIP_RANK_4.getCode();
        } else if (vipPoint >= DIAMONDS_VIP_POINT_STANDARD) {
            shipperVipLevel = VipRankEnum.VIP_RANK_5.getCode();
        }
        return shipperVipLevel;
    }

    /**
     * 根据会员等级进行返现
     *
     * @param vipLevel 会员等级
     * @param money 发货价格
     * @return 返现金额
     */
    private Integer shipperCashBack(Integer vipLevel, Integer money) {
        int cashBack;
        if (VipRankEnum.VIP_RANK_2.getCode().equals(vipLevel)) {
            cashBack = (int) (money * SILVER_VIP_CASH_BACK_PERCENTAGE);
        } else if (VipRankEnum.VIP_RANK_3.getCode().equals(vipLevel)) {
            cashBack = (int) (money * GOLD_VIP_CASH_BACK_PERCENTAGE);
        } else if (VipRankEnum.VIP_RANK_4.getCode().equals(vipLevel)) {
            cashBack = (int) (money * PLATINUM_VIP_CASH_BACK_PERCENTAGE);
        } else {
            cashBack = (int) (money * DIAMONDS_VIP_CASH_BACK_PERCENTAGE);
        }
        return cashBack;
    }


    /**
     * 判断会员过期时间是否在当前时间之后
     *
     * @param vipExpirationTime 会员过期时间
     * @return 判断
     */
    private boolean vipTimeValid(Date vipExpirationTime) {
        return vipExpirationTime.compareTo(new Date()) >= 0;
    }

    /**
     * 计算日期+days之后的日期
     *
     * @param date 日期
     * @param days 天数
     * @return 之后的日期
     */
    private Date dateAddDays(Date date, Integer days) {
        Date vipExpirationTime;
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        vipExpirationTime = calendar.getTime();
        return vipExpirationTime;
    }
}
